Welcome back.
In many places where the Python interpreter is expecting a single value,
but the code provides multiple expressions separated by commas it
automatically packs all those values into a single tuple.
This is just a convenience that makes the code look a little nicer.
It looks like you are working with multiple values,
when really is just making one tuple out of them.
For example, in this code we have several pieces of the information about an actor
Julie Roberts and a movie she was in Duplicity.
We can explicit make those different pieces of information into a tuple
using parenthesis like we do on line number 1.
So, we have open parenthesis there, closed parenthesis,
this is our usual way of creating a tuple.
In between we have a bunch of values there's Julia, Roberts, 1967.
All of these values are separated by commas to indicate where one expression
ends and another begins.
So that's the syntax that you've already seen for creating tuples.
It turns out that line 3 does exactly the same thing.
It's exactly the same except that we've left out the parentheses.
No more parentheses here and it implicitly reads that as,
we got a bunch of values, we gotta pack it into a tuple.
On line 4 you can see as with any tuple, Julia is the name of the variable.
We look up its value, its value is a tuple, square bracket 4 says,
go and get the fifth item, so Julia is 1, 2, 3, 4, 5.
So what it prints out here should be 2009.
So lines 1 and line 3 are just synonyms for each other.
Line 3 is just a way that maybe looks a little bit better as if
you have multiple values.
One place where this is especially useful is when a function wants to return
multiple values.
For example, in this code, circleInfo is a function
and it wants to return two values.
The circumference of the circle, and the area of the circle.
You can only return one value from a Python function, but
that value can be a tuple as we've done here.
So that works fine, I can print the circle information of a circle with radius 10.
And it has a circumference of 62.83,
and an area of 314 But
I also have the option, if I think it looks better to leave the parenthesis out.
And it will automatically return a tuple, and
I'll get exactly the same output, there you go.
So that's tuple packing, we'll see you next time for unpacking.